/**
 * 选择排序
*/

const swap = (arr, i, j) => {
  const temp = arr[i]
  arr[i] = arr[j]
  arr[j] = temp
}

const findKthLargest = (arr, k) => {
  for (let i = 0; i < arr.length - 1; i++) {
    let maxIndex = i
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] > arr[maxIndex]) {
        maxIndex = j
      }
    }
    swap(arr, maxIndex, i)
  }
  return arr[k - 1]
}

console.log(findKthLargest([3, 2, 1, 5, 6, 4], 2))

